package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.concurrent.FutureTask;

@FunctionalInterface
public interface IFunc1<T, TResult> extends IDelegate {
    /*
    + invoke(T t):TResult
+default  invokeAsync(T t,@nullable Callback callback):FutureTask<TResult>

    * */

    TResult invoke(T t) throws Exception;

    default FutureTask<TResult> invokeAsync(T t, @Nullable AsyncCallback callback) {
        FutureTask<TResult> futureTask = new FutureTask<>(() -> {
            TResult result = invoke(t);
            if (callback != null) {
                callback.invoke(this, new Object[]{result});
            }
            return result;
        });

        ASYNC_DELEGATE_THREAD_POOL.submit(futureTask);
        return futureTask;
    }

}
